package com.yet.permissionsxk;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.util.Log;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import com.yet.dialogxk.dialogView.IconDalog;
import com.yet.dialogxk.dialogView.MessageDialog;

/**
 * @ProjectName: My Application
 * @Package: com.yet.permissionsxk
 * @ClassName: PermissionsXK
 * @Description: java类作用描述
 * @Author: liys
 * @CreateDate: 2021/12/17 10:30
 * @UpdateUser: 更新者
 * @UpdateDate: 2021/12/17 10:30
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class PermissionsXK {

   static String TAG ="PermissionsXK";

   public static void requestPermissionXK(AppCompatActivity activity,String[] permissions
   ,int digtitle,int diatext,int ok,int cancel){
       boolean isgranted = checkPermission1(activity,permissions);
       if(!isgranted){
           IconDalog.show(activity, digtitle, diatext, ok, cancel, new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                   requestRightLocation(activity);
               }
           }, new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                   IconDalog.Ondismiss();
               }
           });
       }
   }

    private static boolean checkPermission1(Context context, String[] permissions) {
        PackageManager packageManager = context.getPackageManager();
        String packageName = context.getPackageName();

        for (String permission : permissions) {
            int per = packageManager.checkPermission(permission, packageName);
            if (PackageManager.PERMISSION_DENIED == per) {
                Log.w(TAG, "required permission not granted . permission = " + permission);
                return false;
            }
        }
        return true;
    }


    private static void requestRightLocation(AppCompatActivity context){

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.R){
            ActivityCompat.requestPermissions(context,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    200);
        }
    }

}
